package com.dule.Medic.pages;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.dule.Medic.beans.MedEntryBean;
import com.dule.Medic.model.Doctor;
import com.dule.Medic.model.MedEntry;
import com.dule.Medic.model.Patient;
import com.dule.Medic.service.DoctorService;
import com.dule.Medic.service.MedEntryService;
import com.dule.Medic.service.PatientService;

public class AddMedEntry 
{
	@SessionState      
	@Property    
	private Doctor doctor;     
	private boolean doctorExists;    
	
	@SessionState 
	@Property
	private Patient patient;
	private boolean patientExists;
	
	@Property
	private MedEntry medentry;
	
	@Inject       
	private PatientService patientservice;   
	@Inject       
	private DoctorService doctorservice;   
	@Inject       
	private MedEntryService medentryservice;   
	
	public Object onActivate()
	{   
		if(doctorExists)
		{
			if(patientExists)
			{
				medentry= new MedEntryBean();
				medentry.setDoctor(doctor);
				medentry.setPatient(patient);
				return null;
			}
			else
			{
				return Index.class;
			}
		}        
		return FirstPage.class;   
	}
	
	@OnEvent(component="madentryform")  
	public Object addMedEntry()
	{   
		medentry.setId(medentryservice.save(medentry));
		doctor.getMedEntries().add(medentry);
		patient.getMedEntries().add(medentry);
		patientservice.update(patient);
		doctorservice.update(doctor);
		return ExaminePatient.class;  
	} 
}
